/**
 * 测试工具函数
 */

import { mount, type VueWrapper } from '@vue/test-utils'
import { createPinia, setActivePinia } from 'pinia'
import { createRouter, createWebHistory } from 'vue-router'
import ElementPlus from 'element-plus'
import type { Component } from 'vue'

// 创建测试用的 Pinia 实例
export const createTestPinia = () => {
  const pinia = createPinia()
  setActivePinia(pinia)
  return pinia
}

// 创建测试用的路由实例
export const createTestRouter = () => {
  return createRouter({
    history: createWebHistory(),
    routes: [
      {
        path: '/',
        component: { template: '<div>Test</div>' }
      }
    ]
  })
}

// 创建测试用的挂载函数
export const createTestMount = () => {
  return (component: Component, options: any = {}) => {
    const pinia = createTestPinia()
    const router = createTestRouter()
    
    return mount(component, {
      global: {
        plugins: [pinia, router, ElementPlus],
        ...options.global
      },
      ...options
    })
  }
}

// 等待下一个 tick
export const nextTick = () => new Promise(resolve => setTimeout(resolve, 0))

// 模拟 API 响应
export const mockApiResponse = (data: any, delay = 0) => {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve({ data })
    }, delay)
  })
}

// 模拟 API 错误
export const mockApiError = (message = 'API Error', delay = 0) => {
  return new Promise((_, reject) => {
    setTimeout(() => {
      reject(new Error(message))
    }, delay)
  })
}
